package com.hatricks.is.base.stream.progressinputstream;//package com.glodon.qydata.archive.infrastructure.upload;
//
//import com.glodon.qydata.archive.enums.RedisKeyEnum;
//import com.glodon.qydata.archive.infrastructure.util.RedisUtil;
//import com.glodon.qydata.archive.service.progress.ProgressContext;
//import com.hatricks.base.stream.progressinputstream.ProgressEventType;
//import com.hatricks.base.stream.progressinputstream.ProgressInputStream;
//import lombok.extern.slf4j.Slf4j;
//
//import java.io.InputStream;
//import java.util.concurrent.atomic.AtomicLong;
//import java.util.function.Consumer;
//import java.util.function.Function;
//
///**
// * @packageName: com.glodon.qydata.archive.infrastructure.upload
// * @className: ProgressHelper
// * @author: yanyh yanyh@glodon.com
// * @date: 2024-04-15 16:51
// * @description: 进度条共享
// */
//@SuppressWarnings("UnusedReturnValue")
//@Slf4j
//public class ProgressHelper {
//
//    private static final ThreadLocal<ProgressContext> PROGRESS_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();
//
//    public static ProgressContext startProgress(Long totalSize, Consumer<ProgressContext> consumer) {
//        return startProgress(totalSize, Boolean.FALSE, consumer);
//    }
//
//    public static ProgressContext startProgress(Long totalSize, Boolean controlFinish, Consumer<ProgressContext> consumer) {
//        ProgressContext pc = new ProgressContext(totalSize, controlFinish);
//        pc.startProcess(() -> consumer.accept(pc));
//        PROGRESS_CONTEXT_THREAD_LOCAL.set(pc);
//        return pc;
//    }
//
//    public static ProgressInputStream wrapperProgressInputStream(InputStream inputStream) {
//        return new ProgressInputStream(inputStream, (listener) -> {
//            //log.info("listener:[{}]", listener.getBytes());
//            ProgressContext progressContext = PROGRESS_CONTEXT_THREAD_LOCAL.get();
//            AtomicLong us = progressContext.getUploadedSize();
//            if (!us.compareAndSet(us.get(), listener.getBytes())) {
//                log.error("当前并发导致 进度条更新失败");
//            }
//            if (ProgressEventType.TRANSFER_FINISHED.equals(listener.getEventType())) {
//                progressContext.buildChunkProgress(listener.getBytes());
//            }
//        });
//    }
//
//}
